import mysql from 'mysql2'
import dbConfig from '../config/db'

const pool = mysql.createPool(dbConfig)

const query = (sql: string) => {
    return new Promise<any>((resolve, reject) => {
        pool.getConnection((error, connection) => {
            if (error) {
                console.error(error)
                reject(error)
            } else {
                connection.query(sql, (error, results) => {
                    if (error) {
                        reject(error)
                    } else {
                        resolve(results)
                    }
                    connection.release()
                })
            }
        })
    })
}

export default query
